package org.incubator.text;

import org.incubator.Generator;
import org.incubator.numeric.MinMaxIntegerGenerator;

public class CharacterGenerator implements Generator<Character> {
    private final Generator<Integer> integerGenerator;

    private CharacterGenerator(int start, int end) {
        integerGenerator = new MinMaxIntegerGenerator(start, end);
    }

    public CharacterGenerator(char start, char end) {
        this((int) start, (int) end);
    }

    public static CharacterGenerator all() {
        return new CharacterGenerator(0, 255);
    }

    public static CharacterGenerator numeric() {
        return new CharacterGenerator('0', '9');
    }

    public static CharacterGenerator letter() {
        return new CharacterGenerator('a', 'z');
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Character generate() {
        return asCharacter(integerGenerator.generate());
    }

    private Character asCharacter(Integer integer) {
        if (integer == null) {
            return null;
        }
        return (char) integer.intValue();
    }

}
